// pages/member/favorite/index.js
const app = getApp()
import util from '../../../utils/util'
import {
    list,
    del
} from '../../../api/favorite'

import {
    list as shops
} from '../../../api/shopFavorites'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isShow:false,
        pageNum: 1,
        index: 2,
        list: [],
        shopsForm: []
    },
    loadFood() {
        list({
            pageNum: this.data.pageNum
        }).then(res => {
            res.data.data.forEach(item => {
                item.thumbnail = util.thumbnail(item.thumbnail, 179, 179)
                this.data.list.push(item)
            })
            this.setData({
                list: this.data.list
            })
        })
    },

    loadShop() {
        shops({
            lat: app.globalData.lat,
            lng: app.globalData.lng,
            pageNum: this.data.pageNum
        }).then(res => {
            res.data.data.forEach(item => {
                item.distance = util.rateConverter(item.distance)
                item.score = item.score.toFixed(1)
                item.logo = util.thumbnail(item.logo, 179, 179)
                item.deliverMethod = item.deliverMethod.split("")
                this.data.shopsForm.push(item)
                console.log(this.data.shopsForm)
            })
            this.setData({
                shopsForm: this.data.shopsForm
            })
        })

    },

    change(e) {
        this.data.pageNum=1
      
        this.setData({
            index: e.currentTarget.dataset.index
        })
        if (this.data.index == 1) {
            this.data.list=[]
            this.loadFood()
        } else {
            this.data.shopsForm=[]
            this.loadShop()
        }
    },

    shopClick(e) {
        if(e.currentTarget.dataset.status&&e.currentTarget.dataset.status != 1){
            wx.showToast({
              icon: 'none',
              title: '商户已关闭未开放',
            })
            return
        }
        if(e.currentTarget.dataset.type==2||e.currentTarget.dataset.type==4){
            wx.navigateTo({
                url: '/pages/shop/shopDetail/index?shopId=' + e.currentTarget.dataset.id + '&mchId=' + e.currentTarget.dataset.mchid,
            })
        }else{
            wx.navigateTo({
                url: '/pages/cook/kitchenBrigade/kitchenBrigade?id=' + e.currentTarget.dataset.id + '&mchId=' + e.currentTarget.dataset.mchid,
            })
        }
       
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            index: options.id
        })
        if (options.id == 1) {
            this.loadFood()
        } else {
            this.loadShop()
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.data.pageNum += 1
        if (this.data.index == 1) {
            this.loadFood()
        } else {
            this.loadShop()
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})